app.controller('modulesGoodsAddCtrl',['$scope','$stateParams','$state','$modal','$log','modulesGoodsAddService','utilService','toaster',function($scope,$stateParams,$state,$modal,$log,modulesGoodsAddService,utilService,toaster){
    $scope.id=$stateParams.id;


    $scope.idNull=function(){
        if(angular.isUndefined($stateParams.id)||$stateParams.id==""){
            $state.go('app.modules');
        }
    }

    $scope.idNull();

    //查询商品模块
    $scope.goodsListVO={
        modulesId:$stateParams.id
    };

    $scope.findPageAll=function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'modules/goods/examine',
            params:$scope.goodsListVO
        }).then(function(result){
        });
    }

    $scope.findPageAll();

    //查询店铺名称
    $scope.queryShopName = function(){
        modulesGoodsAddService.queryShopName($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.shopNames=result.data;
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    };

    $scope.queryShopName();

    //添加商品
    $scope.abledState=true;
    $scope.modulesGoods=new Array();

    $scope.addModulesGoods= function (goods) {
        if(!$scope.abledState){
            return;
        }
        $scope.abledState=false;
        modulesGoodsAddService.addModulesGoods($scope,goods.id).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");

                    goods.modulesGoodsId=result.data.id;
                    $scope.modulesGoods.push(goods);
                    $scope.findPageAll();
                    break;
                case 658:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }

            $scope.abledState=true;
        })
    }

    //移除模块商品
    $scope.delModulesGoods= function (goods) {
        modulesGoodsAddService.delModulesGoods($scope,goods.modulesGoodsId).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.modulesGoods.splice($scope.modulesGoods.indexOf(goods),1);
                    $scope.findPageAll();
                    break;
                case 655:
                    toaster.pop('waiting', "操作失败", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }




}]);